package com.ubb.smartsearch.classifier.tool;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.htmlparser.jericho.Source;

import com.ubb.smartsearch.classifier.ClassifierInterface;

/**
 * 
 */

/**
 * @author Aron
 * 
 */
public final class Utils {

	private static Logger logger = Logger.getLogger(Utils.class.getName());

	public static File getTrainFolderOfCorpus(String corpus) throws IOException {

		File corpusFolder = new File(System.getProperty("user.dir")+ "\\corpuses");
		for (File corpusSubFiles : corpusFolder.listFiles()) {
			if (corpusSubFiles.getName().toLowerCase().contains(corpus)) {
				corpusFolder = corpusSubFiles;
				break;
			}
		}

		File trainFolder = null;
		for (File corpusSubFiles : corpusFolder.listFiles()) {
			if (corpusSubFiles.getName().toLowerCase().contains("train")) {
				trainFolder = corpusSubFiles;
				break;
			}
		}

		if (trainFolder == null) {
			throw new TrainDataNotFoundException(corpusFolder.getName());
		}

		return trainFolder;
	}

	public static String extractTextFromURL(String sourceUrlString) {

		Source source = null;
		try {
			source = new Source(new URL(sourceUrlString).openStream());
			source.fullSequentialParse();
			return source.getTextExtractor().setIncludeAttributes(true).toString();
		} catch (MalformedURLException e) {
			logger.severe(e.getMessage());
		} catch (IOException e) {
			logger.severe(e.getMessage());
		}

		return null;
	}

	public static final List<String> tokenize(String data) {
		List<String> matchList = new ArrayList<String>();
		Pattern regex = Pattern.compile("[a-zA-Z][a-zA-Z]+");
		Matcher regexMatcher = regex.matcher(data);
		while (regexMatcher.find()) {
			matchList.add(regexMatcher.group().toLowerCase());
		}
		return matchList;
	}

	public static final String readFile(String path) throws IOException {
		FileInputStream stream = new FileInputStream(new File(path));
		try {
			FileChannel fc = stream.getChannel();
			MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0,
					fc.size());
			return Charset.defaultCharset().decode(bb).toString();
		} finally {
			stream.close();
		}
	}

	public static final void serializeClassifier(ClassifierInterface classifier,
			String filename) throws FileNotFoundException, IOException {

		File trainData = new File("train_data");
		if (!trainData.exists()) {
			trainData.mkdirs();
		}

		File output = new File("train_data\\" + filename);
		if (output.exists()) {
			output.delete();
		}

		FileOutputStream fouts = new FileOutputStream(output);
		ObjectOutputStream out = new ObjectOutputStream(fouts);
		out.writeObject(classifier);
		out.close();
	}

	public static final ClassifierInterface deserializeClassifier(String filename)
			throws FileNotFoundException, IOException, ClassNotFoundException {

		File output = new File("train_data\\" + filename);
		FileInputStream fins = new FileInputStream(output);
		ObjectInputStream in = new ObjectInputStream(fins);
		ClassifierInterface classifier = (ClassifierInterface) in.readObject();
		in.close();
		return classifier;
	}

}
